<template>
  <el-dialog :title="title" :visible="showDialog" :close-on-click-modal="false" @close="closeDialog">
    <el-form ref="form" label-width="120px" :model="queryData" :rules="rules">
      <el-form-item label="标签名称" prop="tagname">
        <el-input v-model="queryData.tagname" style="width:600px" />
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="closeDialog">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { addTagApi, getTagInfoApi, editTagInfoApi } from '@/api/contnet/tag'
export default {
  name: 'TagDialog',
  data() {
    return {
      // dialog 显示隐藏
      showDialog: false,
      // 请求参数
      queryData: {
        tagname: ''
      },
      // 校验
      rules: {
        tagname: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.queryData.id ? '编辑分类' : '新增分类'
    }
  },
  methods: {
    // 关闭dialog
    closeDialog() {
      this.showDialog = false
      this.$refs.form.resetFields()
    },
    // 表单提交
    async btnOK() {
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return console.log(error)
      }
      // 判断是编辑还是新增
      if (this.queryData.id) {
        await editTagInfoApi(this.queryData)
        this.$message.success('修改成功')
      } else {
        await addTagApi(this.queryData)
        this.$message.success('添加成功')
      }
      this.closeDialog()
      this.$emit('updata')
    },
    // 编辑信息回显
    async editTagShow(id) {
      this.showDialog = true
      const res = await getTagInfoApi(id)
      this.queryData = res.data
    }
  }
}
</script>

<style>

</style>
